<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LanguageExport.php 361 2010-02-03 08:08:44Z steen.rabol@gmail.com $
 *
 */

prado::using('System.Xml.TXmlDocument');
Prado::using('Application.Common.Data.PortalLanguageRecord');

class LanguageExport extends PortalAdminPage
{
	private $_langid;
	private	$_mode	= 0;

	public static function ToolMenuName()
	{
		return Prado::localize('Export language');
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Export language');
		if(!isset($this->Request['langid']))
		{
			$this->_mode	= 1;
			$this->pnlImportLanguageSpecific->Enabled	= false;
			$this->pnlImportLanguageSpecific->Visible	= false;

			$this->pnlImportLanguage->Enabled	= true;
			$this->pnlImportLanguage->Visible	= true;

			if(!$this->IsPostBack)
			{
				$this->dlLanglist->PromptValue	= 0;
				$this->dlLanglist->DataSource	= $this->DataAccess->createCommand("select id,name from tbllanguages where active=1")->query();
				$this->dlLanglist->dataBind();
			}
		}
		else
		{
			$this->pnlImportLanguageSpecific->Enabled	= true;
			$this->pnlImportLanguageSpecific->Visible	= true;

			$this->pnlImportLanguage->Enabled	= false;
			$this->pnlImportLanguage->Visible	= false;

			$this->_langid		= TPropertyValue::ensureInteger($this->Request['langid']);

			if(!$this->IsPostBack)
			{
				$langrow	= PortalLanguageRecord::finder()->findByid($this->_langid);
				$this->ltLanguageInfo->Text	= Prado::localize("Export {0} language file", array($langrow->name));
			}
		}
	}

	public function ExportLanguage()
	{
		if($this->_mode == 1)
		{
			$this->_langid	= $this->dlLanglist->SelectedValue;
		}

		$langrow		= PortalLanguageRecord::finder()->findByid($this->_langid);
		$doc			= new TXmlDocument('1.0','utf-8');
		$doc->TagName	= 'langstrings';

 		$proc	= new TXmlElement('language');
 		$proc->setAttribute('culture',$langrow->culture);
 		$doc->Elements[]=$proc;

 		$lstrings	= $this->DataAccess->createCommand("select fromvalue,tovalue,lkey from tbllangstrings where culture='" . $langrow->culture . "' order by fromvalue")->query();
 		$id			= 0;

 		foreach($lstrings as $lstring)
 		{
 			$query				= new TXmlElement('string');
 			$query->setAttribute('id',++$id);
 			$query->setAttribute('lkey',$lstring['lkey']);
 			$proc->Elements[]	= $query;

 			$attr				= new TXmlElement('fromvalue');
 			$attr->Value		= $lstring['fromvalue'];
 			$query->Elements[]	= $attr;

 			$attr				= new TXmlElement('tovalue');
 			$attr->Value		= $lstring['tovalue'];
 			$query->Elements[]	= $attr;
 		}

		PortalUtil::CreateDirStructure($this->Application->PortalBasePath	. '/storage/tmp');
		$this->Response->clear();
		$xmlFileName	= $this->Application->PortalBasePath . '/storage/tmp/' . $langrow->culture . '.xml';
		$doc->saveToFile($xmlFileName);
		$this->Response->writeFile($xmlFileName);
		exit();
	}
}
?>